//
//  introduceViewController.m
//  HotWord
//
//  Created by bean on 11-9-2.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "introduceViewController.h"
#import "HotWordAppDelegate.h"

//#define BASEHEIGHT	284.0f
#define NPAGES		3

@implementation introduceViewController
@synthesize infoPageControl;
@synthesize  introduceScroll;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ 
    return NO; 
} 

//-(void)viewDidAppear:(BOOL)animated
//{
//    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
//    //[[appDelegate.MainToolBar viewWithTag:66] removeFromSuperview];
//    [[appDelegate.MainToolBar viewWithTag:77]removeFromSuperview];
//    [[appDelegate.MainToolBar viewWithTag:66]removeFromSuperview];
//    UIButton *backToMenuBtn=[[UIButton alloc]init];
//    backToMenuBtn.frame=CGRectMake(250, 20, 50, 50);
//    backToMenuBtn.tag=66;
//    [backToMenuBtn addTarget:self action:@selector(retrunBack) forControlEvents:UIControlEventTouchUpInside];
//    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
//    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateHighlighted];
//    [backToMenuBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
//    
//    
//    [appDelegate.MainToolBar addSubview:backToMenuBtn];
//
//
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    
        
    
    
    

}

-(void)viewWillAppear:(BOOL)animated
{

    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    //[[appDelegate.MainToolBar viewWithTag:66] removeFromSuperview];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.2f];
   
    [appDelegate.MainToolBar viewWithTag:77].alpha=0.0f;
    [appDelegate.MainToolBar viewWithTag:66].alpha=0.0f;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(change)];
    [UIView commitAnimations];
    
    
    
    
    
    introduceScroll.contentSize = CGSizeMake(NPAGES * 320.0f, introduceScroll.frame.size.height);
	introduceScroll.delegate = self;
	
	// Load in all the pages
	for (int i = 0; i < NPAGES; i++)
	{
		NSString *filename = [NSString stringWithFormat:@"intro%d.png",i+1];
		UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
		iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, introduceScroll.frame.size.height);
		[introduceScroll addSubview:iv];
		[iv release];
	}
	
	[self.view addSubview:introduceScroll];
	
//	infoPageControl.numberOfPages = 3;
//	infoPageControl.currentPage = 0;
//	[infoPageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
    
    
    
    

}

- (void) pageTurn: (UIPageControl *) aPageControl
{
	int whichPage = aPageControl.currentPage;
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	introduceScroll.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
	[UIView commitAnimations];
}


- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
	CGPoint offset = aScrollView.contentOffset;
	infoPageControl.currentPage = offset.x / 320.0f;
}


-(void)change
{  
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    [[appDelegate.MainToolBar viewWithTag:77]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:66]removeFromSuperview];
    infoPageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(135, 40, 50, 50)];
    infoPageControl.numberOfPages = 3;
	infoPageControl.currentPage = 0;
	[infoPageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
    
    infoPageControl.alpha=0.0f;
    infoPageControl.tag=77;
    infoPageControl.hidden=NO;
    [appDelegate.MainToolBar addSubview:infoPageControl];
    //[appDelegate.MainToolBar bringSubviewToFront:infoPageControl];
    
    UIButton *backToMenuBtn=[[[UIButton alloc]init] autorelease];
    backToMenuBtn.frame=CGRectMake(-50, 20, 100, 100);
    backToMenuBtn.tag=66;
    backToMenuBtn.alpha=0.0f;
    [backToMenuBtn addTarget:self action:@selector(retrunBack) forControlEvents:UIControlEventTouchUpInside];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateHighlighted];
    [backToMenuBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
    [appDelegate.MainToolBar addSubview:backToMenuBtn];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5f];
    backToMenuBtn.alpha=1.0f;
    infoPageControl.alpha=1.0f;
    introduceScroll.frame=CGRectMake(0, 0, 320, 480);
    [UIView setAnimationDelegate:self];
 
    [UIView commitAnimations];

}


-(void)retrunBack
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
